package com.goodfather.textbook.youdao.pad.weight;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import com.goodfather.textbook.youdao.pad.R;


/**
 * Created by XLee on 2017/11/13.
 */

public class AudioSentenceView extends View {

    private float startX = -1;
    private float startY = -1;
    private float endX = -1;
    private float endY = -1;
    private String textContent;//文本内容
    private Paint paint, bgPaint;
    private int mTextHeight, mTextWidth;
    private int mMaskDrawable;

    public void setStartX(float startX) {
        this.startX = startX;
    }

    public void setStartY(float startY) {
        this.startY = startY;
    }

    public void setEndX(float endX) {
        this.endX = endX;
    }

    public void setEndY(float endY) {
        this.endY = endY;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if(startY == -1 || startX == -1 || endY == -1 || endX == -1)  super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        else {
            int widthSpec = MeasureSpec.makeMeasureSpec((int) Math.abs(startX - endX), MeasureSpec.EXACTLY);
            int heightSpec = MeasureSpec.makeMeasureSpec((int) Math.abs(startY - endY), MeasureSpec.EXACTLY);
            setMeasuredDimension(widthSpec, heightSpec);
        }
    }

    public void setTextContent(String textContent) {
        this.textContent = textContent;
        Rect bounds = new Rect();
        this.mTextHeight = bounds.height();
        this.mTextWidth = bounds.width();
    }

    public AudioSentenceView(Context context) {
        this(context, null);
    }

    public AudioSentenceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.sentenceView);
        startX = ta.getInteger(R.styleable.sentenceView_startX, 0);
        startY = ta.getInteger(R.styleable.sentenceView_startY, 0);
        endX = ta.getInteger(R.styleable.sentenceView_endX, 0);
        endY = ta.getInteger(R.styleable.sentenceView_endY, 0);
        textContent = ta.getString(R.styleable.sentenceView_textContent);
        ta.recycle();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 画矩形(RectF)
        if (mMaskDrawable != 0){
            Drawable drawable = getResources().getDrawable(mMaskDrawable);
            drawable.setBounds(0, 0, getWidth(), getHeight());
            drawable.draw(canvas);//设置阴影色
        }else{
            Drawable drawable =getResources().getDrawable(R.drawable.shape_trans_solid) ;
            drawable.setBounds(0, 0, getWidth(), getHeight());
            drawable.draw(canvas);//设置阴影色
        }

    }

    @Override
    public void layout(int l, int t, int r, int b) {
        super.layout(Math.round(startX), Math.round(startY), Math.round(endX), Math.round(endY));
    }

    /** 根据播放进度检查是否要移除*/
    public void checkPlayStatus(int position) {
//        if(getParent() == null)return;
//        long endTime = (long) getTag(R.id.current_endTime);
//        if(position > endTime && endTime != 0) {
//            ((ViewGroup)getParent()).removeView(this);
//        }
    }

    public void removeSelf() {
        if(getParent() == null)return;
        ((ViewGroup)getParent()).removeView(this);
    }

    public void setMaskDrawable(int  resId){
        mMaskDrawable = resId;
        invalidate();
    }
}
